Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./year2.RDS")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.25, n = 498)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 11.36839 11.42481 11.48024 11.53469 11.58816 11.64067 11.69220 11.74278
## [9] 11.79241 11.84109 11.88883 11.93564 11.98152 12.02648 12.07053 12.11368
## [17] 12.15592 12.19727 12.23773 12.27731 12.31600 12.35378 12.39063 12.42655
## [25] 12.46152 12.49553 12.52857 12.56063 12.59170 12.62176 12.65081 12.67883
## [33] 12.70582 12.73175 12.75677 12.78100 12.80441 12.82696 12.84863 12.86939
## [41] 12.88920 12.90804 12.92587 12.94267 12.95841 12.97305 12.98657 12.99893
## [49] 13.01011 13.02008 13.02873 13.03605 13.04208 13.04687 13.05050 13.05301
## [57] 13.05445 13.05490 13.05440 13.05300 13.05077 13.04777 13.04404 13.03965
## [65] 13.03315 13.02327 13.01034 12.99468 12.97662 12.95649 12.93461 12.91131
## [73] 12.88691 12.86174 12.83612 12.81038 12.78484 12.75984 12.73569 12.71272
## [81] 12.69125 12.67163 12.65415 12.63612 12.61497 12.59125 12.56551 12.53830
## [89] 12.51018 12.48169 12.45339 12.42583 12.39957 12.37515 12.35313 12.33406
## [97] 12.31848 12.30473 12.29082 12.27688 12.26301 12.24934 12.23598 12.22304
## [105] 12.21065 12.19891 12.18795 12.17787 12.16880 12.16085 12.15414 12.14932
## [113] 12.14676 12.14616 12.14720 12.14957 12.15297 12.15708 12.16158 12.16618
## [121] 12.17055 12.17439 12.17739 12.17923 12.17961 12.17915 12.17871 12.17835
## [129] 12.17812 12.17807 12.17827 12.17876 12.17960 12.18084 12.18255 12.18476
## [137] 12.18753 12.19093 12.19500 12.19980 12.20538 12.21182 12.21912 12.22724
## [145] 12.23609 12.24563 12.25579 12.26651 12.27773 12.28938 12.30142 12.31378
## [153] 12.32639 12.33920 12.35214 12.36516 12.37819 12.39117 12.40404 12.41675
## [161] 12.43171 12.45091 12.47361 12.49906 12.52651 12.55524 12.58449 12.61353
## [169] 12.64161 12.66800 12.69194 12.71270 12.72954 12.74172 12.75236 12.76492
## [177] 12.77910 12.79463 12.81121 12.82857 12.84641 12.86445 12.88241 12.89999
## [185] 12.91693 12.93292 12.94768 12.96093 12.97239 12.98176 12.98877 12.99312
## [193] 12.99454 12.99273 12.98741 12.97842 12.96601 12.95052 12.93230 12.91170
## [201] 12.88906 12.86473 12.83905 12.81237 12.78503 12.75739 12.72978 12.70256
## [209] 12.67606 12.65064 12.62664 12.59922 12.56411 12.52254 12.47572 12.42488
## [217] 12.37121 12.31596 12.26032 12.20552 12.15278 12.10331 12.05832 12.01905
## [225] 11.98670 11.95632 11.92262 11.88627 11.84799 11.80845 11.76834 11.72835
## [233] 11.68918 11.65151 11.61604 11.58345 11.55444 11.52968 11.50989 11.49364
## [241] 11.47903 11.46596 11.45436 11.44414 11.43523 11.42753 11.42097 11.41546
## [249] 11.41092 11.40727 11.40443 11.40230 11.40082 11.40142 11.40537 11.41237
## [257] 11.42211 11.43426 11.44852 11.46457 11.48209 11.50078 11.52031 11.54038
## [265] 11.56067 11.58086 11.60065 11.61971 11.63774 11.65442 11.66944 11.68247
## [273] 11.69560 11.71088 11.72801 11.74667 11.76654 11.78732 11.80868 11.83031
## [281] 11.85190 11.87314 11.89371 11.91329 11.93158 11.94825 11.96446 11.98146
## [289] 11.99911 12.01727 12.03580 12.05456 12.07340 12.09220 12.11080 12.12907
## [297] 12.14686 12.16404 12.18047 12.19599 12.21075 12.22499 12.23877 12.25212
## [305] 12.26510 12.27776 12.29014 12.30230 12.31429 12.32614 12.33792 12.34966
## [313] 12.36142 12.37325 12.38520 12.39730 12.40975 12.42264 12.43592 12.44951
## [321] 12.46335 12.47738 12.49154 12.50576 12.51998 12.53413 12.54816 12.56199
## [329] 12.57557 12.58882 12.60170 12.61412 12.62603 12.63737 12.64807 12.65807
## [337] 12.66782 12.67776 12.68780 12.69786 12.70784 12.71765 12.72721 12.73642
## [345] 12.74520 12.75346 12.76110 12.76805 12.77421 12.77949 12.78393 12.78761
## [353] 12.79062 12.79303 12.79494 12.79642 12.79757 12.79845 12.79917 12.79980
## [361] 12.80043 12.80114 12.80201 12.80215 12.80078 12.79812 12.79442 12.78993
## [369] 12.78488 12.77951 12.77406 12.76877 12.76389 12.75964 12.75628 12.75405
## [377] 12.75317 12.75256 12.75103 12.74875 12.74586 12.74252 12.73888 12.73508
## [385] 12.73128 12.72763 12.72428 12.72138 12.71909 12.71755 12.71692 12.71734
## [393] 12.71897 12.72225 12.72731 12.73386 12.74156 12.75011 12.75919 12.76849
## [401] 12.77769 12.78649 12.79457 12.80162 12.80731 12.81135 12.81341 12.81480
## [409] 12.81691 12.81960 12.82272 12.82612 12.82963 12.83312 12.83644 12.83942
## [417] 12.84192 12.84379 12.84487 12.84502 12.84408 12.84226 12.83988 12.83700
## [425] 12.83366 12.82992 12.82581 12.82140 12.81672 12.81183 12.80678 12.80161
## [433] 12.79637 12.79111 12.78587 12.78072 12.77569 12.77084 12.76621 12.76184
## [441] 12.75780 12.75347 12.74828 12.74237 12.73589 12.72898 12.72177 12.71440
## [449] 12.70701 12.69975 12.69274 12.68613 12.68005 12.67465 12.67007 12.66643
## [457] 12.66313 12.65949 12.65559 12.65151 12.64732 12.64309 12.63890 12.63482
## [465] 12.63093 12.62731 12.62402 12.62115 12.61876 12.61694 12.61544 12.61397
## [473] 12.61257 12.61126 12.61005 12.60896 12.60802 12.60724 12.60665 12.60627
## [481] 12.60612 12.60622 12.60659 12.60725 12.60823 12.60954 12.61117 12.61309
## [489] 12.61528 12.61774 12.62043 12.62334 12.62645 12.62975 12.63321 12.63681
## [497] 12.64055 12.64440
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.25, n = 498)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 10.89676 10.97036 11.04275 11.11392 11.18386 11.25256 11.32000 11.38617
## [9] 11.45106 11.51467 11.57697 11.63796 11.69763 11.75596 11.81295 11.86857
## [17] 11.92283 11.97571 12.02719 12.07728 12.12595 12.17324 12.21917 12.26376
## [25] 12.30705 12.34905 12.38979 12.42930 12.46760 12.50472 12.54067 12.57550
## [33] 12.60922 12.64185 12.67310 12.70268 12.73068 12.75715 12.78216 12.80578
## [41] 12.82808 12.84912 12.86898 12.88773 12.90542 12.92213 12.93792 12.95287
## [49] 12.96704 12.98050 12.99219 13.00118 13.00775 13.01218 13.01475 13.01575
## [57] 13.01545 13.01414 13.01208 13.00957 13.00689 13.00431 13.00211 13.00058
## [65] 12.99883 12.99585 12.99171 12.98648 12.98021 12.97299 12.96486 12.95591
## [73] 12.94620 12.93580 12.92476 12.91317 12.90109 12.88857 12.87571 12.86255
## [81] 12.84916 12.83562 12.82199 12.80681 12.78888 12.76859 12.74635 12.72256
## [89] 12.69764 12.67198 12.64599 12.62008 12.59465 12.57011 12.54687 12.52532
## [97] 12.50589 12.48526 12.46039 12.43205 12.40102 12.36806 12.33394 12.29944
## [105] 12.26532 12.23237 12.20134 12.17301 12.14816 12.12755 12.11195 12.09854
## [113] 12.08417 12.06913 12.05370 12.03818 12.02286 12.00803 11.99399 11.98101
## [121] 11.96940 11.95945 11.95144 11.94566 11.94242 11.94122 11.94137 11.94279
## [129] 11.94543 11.94921 11.95408 11.95997 11.96682 11.97456 11.98313 11.99247
## [137] 12.00251 12.01319 12.02444 12.03620 12.04841 12.06285 12.08107 12.10269
## [145] 12.12729 12.15449 12.18389 12.21510 12.24772 12.28135 12.31561 12.35008
## [153] 12.38438 12.41812 12.45089 12.48230 12.51196 12.53946 12.56442 12.58644
## [161] 12.60905 12.63556 12.66526 12.69744 12.73138 12.76637 12.80170 12.83665
## [169] 12.87050 12.90255 12.93208 12.95837 12.98071 12.99839 13.01397 13.03037
## [177] 13.04739 13.06484 13.08254 13.10030 13.11793 13.13522 13.15201 13.16809
## [185] 13.18328 13.19738 13.21021 13.22158 13.23130 13.23917 13.24501 13.24862
## [193] 13.24983 13.24843 13.24424 13.23713 13.22727 13.21489 13.20025 13.18359
## [201] 13.16515 13.14516 13.12388 13.10155 13.07841 13.05471 13.03068 13.00657
## [209] 12.98263 12.95909 12.93620 12.91041 12.87862 12.84174 12.80066 12.75629
## [217] 12.70954 12.66130 12.61250 12.56402 12.51678 12.47168 12.42963 12.39152
## [225] 12.35827 12.32535 12.28816 12.24746 12.20403 12.15864 12.11207 12.06508
## [233] 12.01844 11.97293 11.92933 11.88839 11.85089 11.81761 11.78931 11.76275
## [241] 11.73446 11.70491 11.67455 11.64385 11.61326 11.58324 11.55426 11.52677
## [249] 11.50123 11.47810 11.45785 11.44093 11.42780 11.41810 11.41103 11.40642
## [257] 11.40408 11.40382 11.40546 11.40882 11.41372 11.41996 11.42738 11.43578
## [265] 11.44499 11.45481 11.46507 11.47558 11.48616 11.49663 11.50680 11.51648
## [273] 11.52764 11.54203 11.55922 11.57877 11.60025 11.62321 11.64721 11.67183
## [281] 11.69661 11.72112 11.74492 11.76757 11.78864 11.80769 11.82722 11.84976
## [289] 11.87482 11.90193 11.93062 11.96041 11.99084 12.02144 12.05173 12.08123
## [297] 12.10949 12.13602 12.16035 12.18202 12.20259 12.22386 12.24571 12.26799
## [305] 12.29056 12.31330 12.33607 12.35874 12.38117 12.40322 12.42476 12.44565
## [313] 12.46577 12.48498 12.50313 12.52010 12.53617 12.55171 12.56676 12.58134
## [321] 12.59549 12.60924 12.62261 12.63563 12.64833 12.66075 12.67290 12.68482
## [329] 12.69654 12.70808 12.71948 12.73077 12.74196 12.75310 12.76422 12.77533
## [337] 12.78618 12.79653 12.80643 12.81592 12.82505 12.83388 12.84245 12.85081
## [345] 12.85901 12.86711 12.87514 12.88316 12.89121 12.89896 12.90607 12.91261
## [353] 12.91867 12.92432 12.92964 12.93471 12.93959 12.94437 12.94913 12.95394
## [361] 12.95887 12.96401 12.96943 12.97489 12.98010 12.98509 12.98990 12.99456
## [369] 12.99909 13.00353 13.00790 13.01224 13.01658 13.02095 13.02538 13.02989
## [377] 13.03452 13.03934 13.04435 13.04951 13.05479 13.06012 13.06548 13.07080
## [385] 13.07606 13.08119 13.08617 13.09093 13.09544 13.09966 13.10353 13.10701
## [393] 13.11006 13.11344 13.11779 13.12291 13.12856 13.13454 13.14062 13.14659
## [401] 13.15223 13.15732 13.16164 13.16497 13.16709 13.16780 13.16686 13.16476
## [409] 13.16215 13.15907 13.15553 13.15156 13.14720 13.14248 13.13741 13.13204
## [417] 13.12638 13.12047 13.11434 13.10801 13.10152 13.09403 13.08484 13.07412
## [425] 13.06206 13.04884 13.03466 13.01969 13.00411 12.98813 12.97191 12.95564
## [433] 12.93951 12.92371 12.90842 12.89382 12.88009 12.86743 12.85602 12.84604
## [441] 12.83768 12.82994 12.82177 12.81328 12.80460 12.79583 12.78710 12.77852
## [449] 12.77019 12.76225 12.75480 12.74796 12.74184 12.73656 12.73224 12.72898
## [457] 12.72629 12.72360 12.72098 12.71849 12.71619 12.71413 12.71238 12.71101
## [465] 12.71006 12.70960 12.70969 12.71039 12.71177 12.71387 12.71651 12.71946
## [473] 12.72273 12.72636 12.73034 12.73471 12.73946 12.74463 12.75022 12.75626
## [481] 12.76276 12.76974 12.77721 12.78518 12.79369 12.80274 12.81230 12.82238
## [489] 12.83294 12.84399 12.85550 12.86746 12.87985 12.89267 12.90590 12.91952
## [497] 12.93352 12.94789
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.25, n = 498)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 10.87589 10.93409 10.99132 11.04759 11.10290 11.15724 11.21061 11.26301
## [9] 11.31444 11.36489 11.41436 11.46285 11.51035 11.55687 11.60239 11.64692
## [17] 11.69046 11.73301 11.77455 11.81509 11.85464 11.89321 11.93081 11.96744
## [25] 12.00309 12.03776 12.07146 12.10419 12.13594 12.16671 12.19651 12.22533
## [33] 12.25318 12.28005 12.30583 12.33040 12.35381 12.37609 12.39728 12.41741
## [41] 12.43650 12.45461 12.47176 12.48799 12.50332 12.51781 12.53147 12.54434
## [49] 12.55647 12.56788 12.57828 12.58742 12.59536 12.60218 12.60794 12.61272
## [57] 12.61658 12.61960 12.62185 12.62339 12.62430 12.62465 12.62450 12.62393
## [65] 12.62188 12.61739 12.61068 12.60198 12.59151 12.57949 12.56616 12.55173
## [73] 12.53643 12.52048 12.50411 12.48754 12.47100 12.45471 12.43890 12.42378
## [81] 12.40959 12.39655 12.38488 12.37295 12.35918 12.34386 12.32730 12.30981
## [89] 12.29168 12.27321 12.25472 12.23649 12.21884 12.20206 12.18647 12.17235
## [97] 12.16001 12.14827 12.13584 12.12287 12.10951 12.09591 12.08222 12.06858
## [105] 12.05515 12.04207 12.02950 12.01758 12.00647 11.99631 11.98726 11.97913
## [113] 11.97162 11.96466 11.95819 11.95216 11.94649 11.94114 11.93603 11.93110
## [121] 11.92630 11.92156 11.91682 11.91202 11.90710 11.90155 11.89503 11.88776
## [129] 11.87994 11.87178 11.86347 11.85522 11.84724 11.83973 11.83290 11.82695
## [137] 11.82209 11.81851 11.81643 11.81604 11.81756 11.82022 11.82313 11.82634
## [145] 11.82988 11.83379 11.83810 11.84287 11.84812 11.85389 11.86022 11.86715
## [153] 11.87472 11.88296 11.89192 11.90162 11.91212 11.92344 11.93563 11.94873
## [161] 11.96493 11.98592 12.01093 12.03920 12.06996 12.10244 12.13589 12.16953
## [169] 12.20261 12.23435 12.26400 12.29078 12.31393 12.33269 12.35090 12.37262
## [177] 12.39740 12.42481 12.45440 12.48573 12.51837 12.55186 12.58578 12.61968
## [185] 12.65312 12.68565 12.71684 12.74625 12.77343 12.79795 12.81936 12.83723
## [193] 12.85110 12.86055 12.86513 12.86537 12.86230 12.85625 12.84754 12.83649
## [201] 12.82342 12.80866 12.79252 12.77532 12.75740 12.73906 12.72063 12.70243
## [209] 12.68479 12.66802 12.65245 12.63361 12.60758 12.57547 12.53841 12.49751
## [217] 12.45390 12.40868 12.36299 12.31794 12.27464 12.23423 12.19781 12.16651
## [225] 12.14145 12.11863 12.09358 12.06675 12.03856 12.00943 11.97979 11.95008
## [233] 11.92071 11.89212 11.86474 11.83898 11.81529 11.79409 11.77580 11.75916
## [241] 11.74270 11.72650 11.71066 11.69528 11.68045 11.66626 11.65281 11.64020
## [249] 11.62851 11.61783 11.60828 11.59993 11.59289 11.58766 11.58459 11.58349
## [257] 11.58421 11.58656 11.59038 11.59550 11.60175 11.60894 11.61693 11.62552
## [265] 11.63456 11.64386 11.65327 11.66260 11.67169 11.68037 11.68846 11.69580
## [273] 11.70355 11.71283 11.72343 11.73513 11.74771 11.76095 11.77464 11.78855
## [281] 11.80246 11.81616 11.82942 11.84203 11.85377 11.86442 11.87476 11.88569
## [289] 11.89711 11.90897 11.92117 11.93365 11.94632 11.95911 11.97194 11.98474
## [297] 11.99742 12.00992 12.02215 12.03405 12.04678 12.06142 12.07767 12.09524
## [305] 12.11386 12.13324 12.15310 12.17315 12.19311 12.21269 12.23161 12.24959
## [313] 12.26633 12.28157 12.29501 12.30637 12.31624 12.32544 12.33401 12.34197
## [321] 12.34936 12.35623 12.36261 12.36854 12.37404 12.37917 12.38395 12.38842
## [329] 12.39262 12.39659 12.40036 12.40397 12.40745 12.41085 12.41419 12.41753
## [337] 12.42040 12.42242 12.42373 12.42444 12.42470 12.42462 12.42433 12.42395
## [345] 12.42363 12.42348 12.42363 12.42422 12.42536 12.42634 12.42645 12.42583
## [353] 12.42461 12.42293 12.42092 12.41872 12.41646 12.41429 12.41234 12.41073
## [361] 12.40962 12.40913 12.40940 12.40948 12.40846 12.40653 12.40392 12.40080
## [369] 12.39740 12.39390 12.39052 12.38746 12.38490 12.38307 12.38216 12.38237
## [377] 12.38391 12.38681 12.39089 12.39599 12.40195 12.40864 12.41590 12.42358
## [385] 12.43152 12.43959 12.44763 12.45549 12.46301 12.47006 12.47647 12.48211
## [393] 12.48681 12.49180 12.49824 12.50584 12.51435 12.52348 12.53297 12.54254
## [401] 12.55192 12.56085 12.56905 12.57625 12.58218 12.58657 12.58914 12.59071
## [409] 12.59222 12.59365 12.59496 12.59612 12.59708 12.59781 12.59828 12.59845
## [417] 12.59829 12.59775 12.59682 12.59543 12.59358 12.59073 12.58648 12.58099
## [425] 12.57439 12.56683 12.55845 12.54939 12.53981 12.52984 12.51963 12.50931
## [433] 12.49904 12.48896 12.47921 12.46993 12.46128 12.45339 12.44640 12.44046
## [441] 12.43572 12.43155 12.42727 12.42292 12.41853 12.41413 12.40978 12.40550
## [449] 12.40134 12.39732 12.39350 12.38989 12.38656 12.38352 12.38082 12.37849
## [457] 12.37636 12.37421 12.37208 12.36999 12.36796 12.36602 12.36420 12.36252
## [465] 12.36101 12.35969 12.35859 12.35773 12.35714 12.35684 12.35675 12.35677
## [473] 12.35690 12.35716 12.35756 12.35810 12.35880 12.35965 12.36068 12.36189
## [481] 12.36329 12.36489 12.36669 12.36871 12.37094 12.37339 12.37604 12.37889
## [489] 12.38194 12.38517 12.38858 12.39216 12.39592 12.39984 12.40392 12.40815
## [497] 12.41252 12.41704
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")
keeping in case
#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")